import java.util.ArrayList;
import java.util.Arrays;

public class L501 {

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(1);
        TreeNode treeNode2 = new TreeNode(1);
        TreeNode treeNode3 = new TreeNode(0);
        TreeNode treeNode4 = new TreeNode(0);

        treeNode.left = treeNode2;
        treeNode2.left = treeNode3;
        treeNode3.left = treeNode4;

        int[] mode = new Solution().findMode(treeNode);
        System.out.println(Arrays.toString(mode));
    }

    static class Solution {

        int count = 1;
        int countMax = 1;
        int pre = Integer.MAX_VALUE;
        ArrayList<Integer> result = new ArrayList<Integer>();

        public int[] findMode(TreeNode root) {
            find(root);
            int[] ints = new int[result.size()];
            for (int i = 0; i < result.size(); i++) {
                ints[i] = result.get(i);
            }
            return ints;
        }

        private void find(TreeNode root) {
            if (root == null) {
                return;
            }

            find(root.left);

            if (pre == root.val) {
                count++;
            } else {
                count = 1;
            }

            if (countMax < count) {
                result.clear();
                result.add(root.val);
                countMax = count;
            } else if (countMax == count) {
                result.add(root.val);
            }
            pre = root.val;

            find(root.right);
        }
    }
}
